//
//  BChargeCashChargeAddressView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/7.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BChargeCashChargeAddressView.h"

#import "UserWalletChargeResponse.h"
//生成二维码
#import "SGQRCode.h"

static CGFloat BChargeCashChargeAddressViewTopPadding = 10.0;
static CGFloat BChargeCashChargeAddressViewAddressPadding = 8.0;
static CGFloat BChargeCashChargeAddressViewQRPadding = 20.0;

@interface BChargeCashChargeAddressView ()

@property (nonatomic, strong) QMUILabel *currencyLabel;

@property (nonatomic, strong) QMUILabel *addressLabel;

/**
 *qrImageView
 */
@property (nonatomic, strong) UIImageView *qrImageView;

@end

@implementation BChargeCashChargeAddressView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.currencyLabel = [QMUILabel new];
    self.currencyLabel.textAlignment = NSTextAlignmentCenter;
    self.currencyLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.currencyLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    self.currencyLabel.numberOfLines = 0;
    [self addSubview:self.currencyLabel];
    [self.currencyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(BChargeCashChargeAddressViewTopPadding);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self).offset(-40.0);
    }];
    //
    self.addressLabel = [QMUILabel new];
    self.addressLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.addressLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    self.addressLabel.numberOfLines = 0;
    self.addressLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.addressLabel];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.currencyLabel.mas_bottom).offset(BChargeCashChargeAddressViewAddressPadding);
        make.centerX.mas_equalTo(self);
        make.width.mas_lessThanOrEqualTo(self).offset(-40.0);
    }];
    
    self.qrImageView = [UIImageView new];
    self.qrImageView.backgroundColor = [UIColor generalBackgroundColor];
    [self addSubview:self.qrImageView];
    [self.qrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.addressLabel.mas_bottom).offset(BChargeCashChargeAddressViewQRPadding);
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo([self.class qrIVSize]);
    }];
}


//
- (CGFloat)getAddressContentHeight:(UserWalletChargeData *)chargeData {
    CGFloat height = 0;
    if (chargeData) {
        self.currencyLabel.text = chargeData.flow.addr;
        self.addressLabel.text = chargeData.address;
        NSString *address = chargeData.address;
        if (address.length) {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                UIImage *qrImage = [SGQRCodeGenerateManager SG_generateWithDefaultQRCodeData:address imageViewWidth:[self.class qrIVSize].width];
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.qrImageView.image = qrImage;
                });
            });
        }else {
            self.qrImageView.image = nil;
        }
        
        [self layoutIfNeeded];
        //
        height += BChargeCashChargeAddressViewTopPadding;
        height += CGRectGetHeight(self.currencyLabel.frame);
        height += BChargeCashChargeAddressViewAddressPadding;
        height += CGRectGetHeight(self.addressLabel.frame);
        height += BChargeCashChargeAddressViewQRPadding;
        height += [self.class qrIVSize].height;
        height += 10.0;
    }
    return height;
}

- (UIImage *)qrImage {
    return self.qrImageView.image;
}

+ (CGSize)qrIVSize {
    return CGSizeMake(225.0, 225.0);
}

@end
